
//
//  MBNetTool.m
//  Zuker
//
//  Created by MB__Lee on 2018/5/10.
//  Copyright © 2018年 zuke.com. All rights reserved.
//

#import "MBNetTool.h"


#import <AFNetworking/AFNetworking.h>

@interface MBNetTool()


@property(nonatomic,strong)AFHTTPSessionManager *manager;

@end


@implementation MBNetTool

-(AFHTTPSessionManager *)manager{
    if (!_manager) {
      
        
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        _manager = manager;
        manager.responseSerializer = [AFJSONResponseSerializer serializer];//设置返回数据为json
        manager.requestSerializer = [AFHTTPRequestSerializer serializer];// 请求
        
        manager.requestSerializer.stringEncoding = NSUTF8StringEncoding;
        manager.requestSerializer.timeoutInterval= 60;
        manager.responseSerializer.acceptableContentTypes = [NSSet setWithArray:@[@"application/json",
                                                                                  @"text/html",
                                                                                  @"text/json",
                                                                                  @"text/plain",
                                                                                  @"text/javascript",
                                                                                  @"text/xml",
                                                                                  @"image/*"]];
    }
    return  _manager;
}
+(void)MBPost:(NSString *)url paramater:(NSMutableDictionary *)dic success:(MBSuccess)success failure:(MBFailure)failure{
    [[[self alloc] init] MBPost:url paramater:dic success:success failure:failure];
}

-(void)MBPost:(NSString *)url paramater:(NSDictionary *)dic success:(MBSuccess)success failure:(MBFailure)failure{
    NSString *str = [ReturnSign returnSignWithDic:dic];
    NSString *md5String = [NSString stringWithFormat:@"%@%@%@",str,APPKEY_URL,url];
    md5String = [LCMD5Tool MD5ForLower32Bate:md5String];
    NSMutableDictionary * param = [NSMutableDictionary dictionaryWithDictionary:dic];
    [param setObject:md5String forKey:@"sign"];
    
    
//    [self MBSessionRequestWithString:md5String url:url dic:dic];
    
//    __weak typeof(self)WeakSelf = self;
//    [AFHttpTool requestWihtUrl:url params:param success:^(id response) {
////        NSLog(@"param=====%@",param);
//        success(response);
//    } failure:^(NSError *err) {
//        failure(err);
//
//    }];
    [self.manager POST:url parameters:param progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        success(responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failure(error);
    }];


}

-(void)MBSessionRequestWithString:(NSString *)string url:(NSString *)url dic:(NSDictionary *)dic{
    NSURLSession *session = [NSURLSession sharedSession];
    
    
    NSArray *arr = [dic allKeys];
    NSMutableArray *mut = [NSMutableArray array];
    for (NSString *key in arr) {
        
        NSString *name = [NSString stringWithFormat:@"%@=%@",key,dic[key]];
        [mut addObject:name];
    }
    NSString *allSting = [mut componentsJoinedByString:@"&"];
    allSting = [NSString stringWithFormat:@"%@&sign=%@",allSting,string];
    
    
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
    request.HTTPMethod = @"POST";
    request.HTTPBody = [allSting dataUsingEncoding:4];
    
    NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
         NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableLeaves) error:nil];
        
        
    }];
    
    [task resume];
    
   
    
}




#pragma mark --鹏飞的成功案例
//- (void)loadDataForMap
//{
//    NSString *url = [NSString stringWithFormat:@"%@map/around",Main_Url];
//    NSDictionary *dic = @{
//                          @"category" : @"1001",
//                          @"lat" : @"22.541826407628456",
//                          @"lng" : @"113.991471831776",
//                          @"radius" : @"30000"
//                          };
//    NSString *str = [ReturnSign returnSignWithDic:dic];
//    NSString *md5String = [NSString stringWithFormat:@"%@%@%@",str,APPKEY_URL,url];
//    md5String = [LCMD5Tool MD5ForLower32Bate:md5String];
//    NSMutableDictionary * param = [NSMutableDictionary dictionaryWithDictionary:dic];
//    [param setObject:md5String forKey:@"sign"];
//    __weak typeof(self)WeakSelf = self;
//    NSLog(@"%@",param);
//    [AFHttpTool requestWihtUrl:url params:param success:^(id response) {
//        if ([response[@"code"] isEqualToString:@"10000"]) {
//            NSDictionary *dic = response[@"data"];
//
//
//        }else {
//            //            [APPDELEGATE showPro:response[@"message"]];
//        }
//    } failure:^(NSError *err) {
//
//        //        [WeakSelf.ListView stopRefresh];
//    }];
//}


@end

